﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Planer.Model.Validators;
using Planer.Model.Validators.Base;

namespace Planer.Model
{

    public class UserModel
    {
        static UserModel model = null;       

        protected UserModel()
        {            
        }

        public static UserModel GetInstance()
        {
            if (model == null)
            {
                model = new UserModel();
            }

            return model;
        }

        public List<user> GetUserList()
        {
            List<user> users = new List<user>();

            var query = from us in Program.Db.users
                        select us;
            users.AddRange(query.ToList<user>());

            return users;
        }

        public ModelResult SaveOrUpdateUser(user User)
        {
            UserValidator validator = new UserValidator();            

            validator.Validate(User);
            if (validator.Status == OperationStatusType.ERROR)
            {
                return new ModelResult(validator.Errors);
            }

            if (User.id == 0)
            {
                Program.Db.AddTousers(User);
            }

            Program.Db.SaveChanges();

            return new ModelResult();
        }
        
    }
}
